package com.dmp.onlinestore.controller;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dmp.onlinestore.system.Settings;
import com.dmp.onlinestore.view.base.ViewAddressFactory;

/**
 * Servlet implementation class Test
 */
public class Controller extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Controller() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}
	
	protected void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (Settings.CONSOLE_DEBUG_ON) {
			// debug...
			String action = (String) request.getParameter("action");
			//String id = (String) request.getParameter("id");
			System.out.println("action is \"" + action + "\"");
			//System.out.println("id is \"" + id + "\"");
		}

		//boolean isError = false;
		try {
			ActionHandler handler = ActionHandlerFactory.instance().getActionHandler(request, response);
			handler.handle();
		}
		catch (ControllerUnknownActionException e) {
			showPageNotFound(request, response);
		}
		catch (ControllerException e) {
			// Dispatch the generic JSP error page here...
			System.out.println("Controller exception: \"" + e.getErrorString() + "\"");
			e.printStackTrace();
			showGeneralErrorPage(request, response);
			//isError = true;
		}
		catch (RuntimeException e) {
			// Catch all runtime exceptions so that we give the user a clean error page.
			System.out.println("Caught the runtime exception!!");
			e.printStackTrace();
			showGeneralErrorPage(request, response);
			//isError = true;
		}
//		finally {
//			if (isError) {
//				showGeneralErrorPage(request, response);
//			}
//		}
	}
	
	// Something went wrong. Beg the user for their forgiveness.
	private void showGeneralErrorPage(HttpServletRequest request, HttpServletResponse response) {
	    String viewPageAddress = ViewAddressFactory.instance().getGeneralError();
		RequestDispatcher dispatcher = request.getRequestDispatcher(viewPageAddress);
		try {
			dispatcher.forward(request, response);
		}
		catch (ServletException e) {
			// Give up.
			e.printStackTrace();
		}
		catch (IOException e) {
			// Give up.
			e.printStackTrace();
		}
	}

	// Something went wrong. Beg the user for their forgiveness.
	private void showPageNotFound(HttpServletRequest request, HttpServletResponse response) {
	    String viewPageAddress = ViewAddressFactory.instance().getPageNotFound();
		RequestDispatcher dispatcher = request.getRequestDispatcher(viewPageAddress);
		try {
			dispatcher.forward(request, response);
		}
		catch (ServletException e) {
			// Give up.
			e.printStackTrace();
		}
		catch (IOException e) {
			// Give up.
			e.printStackTrace();
		}
	}
}
